<template>
  <div>
    <div class="operation">
      <div style="margin:10px 10px">
        <el-input style="width: 200px" placeholder="请输入名称" suffix-icon="el-icon-search" v-model="username"></el-input>
        <!--        <el-input style="width: 200px" placeholder="请输入邮箱" suffix-icon="el-icon-message" class="ml-5" v-model="email"></el-input>-->

        <el-button class=ml-5 type="primary" @click="load">搜索</el-button>
        <el-button type="warning" @click="reset">重置</el-button>
      </div>
      <div style="margin:10px 0px">
        <el-popconfirm
            class="ml-5"
            confirm-button-text='确定'
            cancel-button-text='取消'
            icon="el-icon-info"
            icon-color="red"
            title="您确定批量删除这些数据吗？"
            @confirm="delBatch"
        >
          <el-button type="danger" slot="reference">批量删除<i class="el-icon-remove-outline"></i></el-button>
        </el-popconfirm>
      </div>
    </div>
    <el-table :data="usernameList" border stripe header-cell-class-name="headerBg"
              @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column prop="id" label="id" width="300" align="center"></el-table-column>
      <el-table-column prop="username" label="用户名" width="140" align="center"></el-table-column>
      <el-table-column prop="nickname" label="昵称" width="120" align="center"></el-table-column>
      <el-table-column prop="password" label="密码" width="200" align="center"></el-table-column>
      <el-table-column prop="mail" label="邮箱" align="center"></el-table-column>
      <el-table-column prop="phone" label="电话" width="160" align="center"></el-table-column>
      <el-table-column prop="avatar" label="头像" width="77" align="center">
        <template slot-scope="scope">
          <el-avatar shape="circle" :size="50" :src="scope.row.avatar"></el-avatar>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="120" align="center">
        <template slot-scope="scope">
          <!--                <el-button type="success" @click="handleEdit(scope.row)">编辑<i class="el-icon-edit"></i></el-button>-->
          <el-popconfirm
              class="ml-5"
              confirm-button-text='确定'
              cancel-button-text='取消'
              icon="el-icon-info"
              icon-color="red"
              title="确定删除吗？"
              @confirm="del(scope.row.id)"
          >
            <el-button type="danger" slot="reference">删除<i class="el-icon-delete"></i></el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <div style="padding: 10px 0">
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageNum"
          :page-sizes="[5, 10, 20]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>
      <!--<div v-for="item in usernameList">
          {{item.nickname}}
      </div>-->
    </div>
    <el-dialog title="用户信息" :visible.sync="dialogFormVisible" width="30%" align="center">
      <el-form label-width="80px" size="small">
        <el-form-item label="用户名">
          <el-input v-model="form.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="昵称">
          <el-input v-model="form.nickname" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="邮箱">
          <el-input v-model="form.email" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="电话">
          <el-input v-model="form.phone" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="头像">
          <el-input v-model="form.avatar" autocomplete="off"></el-input>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import axios from "axios";

export default {
  name: "User",
  data() {
    return {
      tableData: [],
      total: 0,
      pageNum: 1,
      pageSize: "5",
      username: "",
      nickname: "",
      password: "",
      email: "",
      avatar: "",
      circleUrl: "",
      dialogFormVisible: false,
      multipleSelection: [],
      form: {},
      headerBg: 'headerBg',
      usernameList: [],
    }
  },
  created() {
    this.load();
    // console.log("查看token", localStorage.getItem("Token"))
    /*this.getData();*/
  },
  methods: {
    getData() {
      axios({
        method: "GET",
        url: this.$e.Url + "/admin/getAllUsers",
      }).then((res) => {
        console.log("请求API接口【/admin/getAllUsers】", res);
        this.usernameList = res.data;
      })
    },
    del(id) {
      axios({
        url: this.$e.Url + "/admin/checkToken",
        params: {
          Token: localStorage.getItem("Token")|| ""
        },
        method: "GET"
      }).then(ans => {
        console.log("请求API接口【/admin/checkToken】", ans);
        if (ans.data.result) {
          axios({
            url: this.$e.Url + "/user/deleteMyself",
            method: "DELETE",
            data: id
          }).then((res) => {
            console.log("请求API接口【/user/deleteMyself】", res);
            if (res) {
              this.$message.success("删除成功")
            } else {
              this.$message.error("删除失败")
            }
            this.load();
          })
        } else {
          this.$message.error("身份过期,权限不够");
        }
      })
    },

    load() {
      axios.get(this.$e.Url + "/admin/FindPage", {
            params: {
              pageNum: this.pageNum,
              pageSize: this.pageSize,
              username: this.username,

            }
          }
      ).then(res => {
        console.log(res)
        this.usernameList = res.data.data
        this.total = res.data.total
        /* this.usernameList=res.data;*/
      })
      //已有request不用fetch

      /*fetch("http://localhost:9090/user/page?pageNum="+this.pageNum+"&pageSize="+this.pageSize +"&username=" + this.username)
              .then(res =>res.json()).then(res=>{
        console.log(res)
        this.tableData=res.data
        this.total=res.total
      })*/
    },
    handleAdd() {
      this.dialogFormVisible = true
      this.form = {}
    },
    save() {
      this.request.post("/user", this.form).then(res => {
        if (res) {
          this.$message.success("保存成功")
          this.dialogFormVisible = false
          this.load()
        } else {
          this.$message.error("保存失败")
        }
      })
    },
    handleSelectionChange(val) {
      console.log(val)
      this.multipleSelection = val

    },
    delBatch() {
      axios({
        url: this.$e.Url + "/admin/checkToken",
        params: {
          Token: localStorage.getItem("Token")|| ""
        },
        method: "GET"
      }).then(ans => {
        console.log("请求API接口【/admin/checkToken】", ans);
        if (ans.data.result) {
          let ids = this.multipleSelection.map(v => v.id)  // 获取所有需要删除的id
          let deletePromises = ids.map(id => {  // 将每个删除操作转换为一个Promise
            return axios({
              url: this.$e.Url + "/user/deleteMyself",
              method: "DELETE",
              data: id
            })
          });
          console.log("删除信息", deletePromises)
          Promise.all(deletePromises).then(responses => {  // 等待所有删除操作完成
            // 检查所有删除操作的响应，如果有任何一个响应表示删除失败，则认为批量删除失败
            let success = responses.every(response => response.status === 200);
            if (success) {
              this.$message.success("批量删除成功");
            } else {
              this.$message.error("批量删除失败");
            }
            this.load();
          });

        } else {
          this.$message.error("身份过期,权限不够");
        }
      })

    },
    handleEdit(row) {
      this.form = row
      this.dialogFormVisible = true
    },
    reset() {
      this.username = ""
      this.load()
    },
    handleSizeChange(pageSize) {
      console.log(pageSize)
      this.pageSize = pageSize
      this.load()
    },
    handleCurrentChange(pageNum) {
      console.log(pageNum)
      this.pageNum = pageNum
      this.load()
    }
  }
}
</script>

<style>
.headerBg {
  background: #cccccc !important;

}

.operation {
  display: flex;
}
</style>
